<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015-8-25
 * Time: 10:13
 */

namespace common\components\services\timedtask;

use common\models\datas\CardAttr;
use common\models\datas\Classhourfee;
use common\components\helpers\Fun;
use common\models\datas\Customer;
use common\models\datas\Site;
use frontend\models\datas\ClassLesson;
use frontend\models\datas\Goods;
use frontend\models\datas\GoodsAttr;
use frontend\models\datas\Mechanic;
use common\models\datas\Student;
use yii\base\Component;
use yii\helpers\ArrayHelper;

class ClasshourfeeHelper extends Component
{
   //写入课时费表
    public function send()
    {
        $yesterdayTime = strtotime(date("Y-m-d",strtotime("-1 day")));//前一天开始时间戳
        $todayTime = time();//前一天结束时间戳
        $site = Site::find()->indexBy('domain')->asArray()->all();
        $dbs = \Yii::$app->params['db'];
        if($dbs) {
            foreach($dbs as $key=>$db) {
                if(!empty($db)) {
                    $check_list = (new \yii\db\Query())
                                 ->select(['sc.*','oi.mechanic_id','oi.goods_id','o.customer_id','cs.class_manage_id'])
                                 ->from('{{%student_check}} sc')
                                 ->leftJoin('{{%orders_items}} oi','sc.order_sales_id = oi.id')
                                 ->leftJoin('{{%orders}} o','o.id = oi.orders_id')
                                 ->leftJoin('{{%class_student}} cs','cs.id = sc.class_student_id')
                                 ->where(['between','sc.created_at',$yesterdayTime,$todayTime])
                                 ->andWhere(['sc.status'=>1])
                                 ->andWhere(['o.order_type'=>'appointment'])
                                ->all(\Yii::createObject($db));
                    if($check_list){
                        foreach($check_list as $check){
                            $row =  Classhourfee::find()->where(['customer_id'=>$check['customer_id'],'student_id'=>$check['student_id'],'lesson_id'=>$check['class_lesson_id'],'class_id'=>$check['class_manage_id'],'site_id'=>$site[$key]['id']])->one();
                            if($row == null) {
                                $ClasshourfeeModel = new Classhourfee();
                                $ClasshourfeeModel->customer_id = $check['customer_id'];
                                $ClasshourfeeModel->student_id = $check['student_id'];
                                $ClasshourfeeModel->mechanic_id = $check['mechanic_id'];
                                $ClasshourfeeModel->class_id = $check['class_manage_id'];
                                $ClasshourfeeModel->class_student_id = $check['class_student_id'];
                                $ClasshourfeeModel->lesson_id = $check['class_lesson_id'];
                                $ClasshourfeeModel->goods_id = $check['goods_id'];
                                $ClasshourfeeModel->site_id = $site[$key]['id'];
                                $ClasshourfeeModel->created_at = time();

                                $mechanic = (new \yii\db\Query())->select('*')->from('{{%mechanic}}')->where(['id' => $check['mechanic_id']])->one(\Yii::createObject($db));
                                $goods = (new \yii\db\Query())->select('*')->from('{{%goods}}')->where(['id' => $check['goods_id']])->one(\Yii::createObject($db));
                                $classroom = (new \yii\db\Query())->select('*')->from('{{%goods_attr}}')->where(['goods_id' => $check['goods_id'], 'obj_type' => 'classroom_name'])->one(\Yii::createObject($db));
                                $teacher = (new \yii\db\Query())->select('*')->from('{{%goods_attr}}')->where(['goods_id' => $check['goods_id'], 'obj_type' => 'teacher'])->one(\Yii::createObject($db));
                                $class_lesson = (new \yii\db\Query())->select('*')->from('{{%class_lesson}}')->where(['lesson_id' => $check['class_lesson_id']])->one(\Yii::createObject($db));
                                $class_count = (new \yii\db\Query())->select('*')->from('{{%class_lesson}}')->where(['class_id' => $check['class_manage_id']])->count('*', \Yii::createObject($db));

                                $customer = Customer::findOne($check['customer_id']);
                                $student = Student::find()->where(['id' => $check['student_id']])->one();

                                $cost = 0;
                                if (!empty($goods) && $class_count > 0) {
                                    $cost = floor($goods['cost'] / $class_count);
                                }

                                $student_age = 0;
                                if (!empty($student) && $student->age != null) {
                                    $student_age = date('Y') - $student->age;
                                }
                                $salesman = CardAttr::find()->where(['customer_id' => $check['customer_id']])->one();


                                $ClasshourfeeModel->mechanic_name = !empty($mechanic) ? $mechanic['name'] : '';
                                $ClasshourfeeModel->goods_name = !empty($goods) ? $goods['name'] : '';
                                $ClasshourfeeModel->cost = $cost;
                                $ClasshourfeeModel->classroom_id = !empty($classroom) ? $classroom['attribute_id'] : 0;
                                $ClasshourfeeModel->classroom_name = !empty($classroom) ? $classroom['value'] : '';
                                $ClasshourfeeModel->teacher_id = !empty($teacher) ? $teacher['attribute_id'] : 0;
                                $ClasshourfeeModel->teacher_name = !empty($teacher) ? $teacher['value'] : '';
                                $ClasshourfeeModel->class_date = !empty($class_lesson) ? $class_lesson['class_date'] : 0;
                                $ClasshourfeeModel->class_day = !empty($class_lesson) ? date('w', $class_lesson['class_date']) : 0;
                                $ClasshourfeeModel->class_start = !empty($class_lesson) ? $class_lesson['start_time'] : '';
                                $ClasshourfeeModel->class_end = !empty($class_lesson) ? $class_lesson['end_time'] : '';
                                $ClasshourfeeModel->customer_name = !empty($customer) ? $customer->name : '';
                                $ClasshourfeeModel->student_name = !empty($student) ? $student->name : '';
                                $ClasshourfeeModel->cate_id = !empty($goods) ? $goods['cat_id'] : 0;
                                $ClasshourfeeModel->mechanic_area = !empty($mechanic) ? $mechanic['region'] : '';
                                $ClasshourfeeModel->salesman_id = !empty($salesman) ? $salesman->salesman_id : 0;
                                $ClasshourfeeModel->student_age = $student_age;

                                $ClasshourfeeModel->save(false);
                            }

                        }
                    }
                }
            }
        }
    }
}